package com.musictab.test.Decryption;

import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 *  文件操作工具
 */
public class ReadFileUtils {

	/**
	 * 读取某个文件夹下的所有文件
	 */
	public static File[] readfile(String filepath, String regex) {

		File file = new File(filepath);

		//如果传入的路径是目录
		if (file.isDirectory()) {

			if (StringUtils.isNotBlank(regex))
				return file.listFiles(filter(regex));

			return file.listFiles();
		}

		return new File[] { file };

	}

	//过滤器
	private static FilenameFilter filter(final String regex) {

		return new FilenameFilter() {

			private final Pattern pattern = Pattern.compile(regex);

			@Override
			public boolean accept(File dir, String name) {

				return pattern.matcher(name).matches();
			}
		};
	}

}